/* Copyright 2011 Google Inc. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.apps.easyconnect.demos.easyrpbasic.web.servlet;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.apps.easyconnect.demos.easyrpbasic.web.data.User;
import com.google.apps.easyconnect.demos.easyrpbasic.web.data.UserDAO;
import com.google.apps.easyconnect.demos.easyrpbasic.web.data.orm.ORMUtil;
import com.google.apps.easyconnect.demos.easyrpbasic.web.util.Constants;

/**
 * Handles the User management request. Currently only support delete User request.
 * 
 * @author guibinkong@google.com (Guibin Kong)
 */
public class AccountServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  private static final Logger log = Logger.getLogger(AccountServlet.class.getName());

  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    doPost(req, resp);
  }

  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String action = req.getParameter("action");
    log.entering("AccountServlet", "doPost", action);

    if ("delete".equals(action)) {
      HttpSession session = req.getSession(false);
      if (session != null) {
        User user = (User) session.getAttribute(Constants.SESSION_KEY_LOGIN_USER);
        if (user != null) {
          session.removeAttribute(Constants.SESSION_KEY_LOGIN_USER);
          UserDAO.delete(user.getEmail());
          log.info("Successfully delete user [" + user.getEmail() + "].");
        }
      }
      resp.sendRedirect(Constants.HOME_PAGE_URL);
    } else if ("edit".equals(action)) {
      HttpSession session = req.getSession(false);
      if (session != null) {
        User user = (User) session.getAttribute(Constants.SESSION_KEY_LOGIN_USER);
        if (user != null) {
          User newProfile = ORMUtil.parseUser(req, "");
          newProfile.setEmail(user.getEmail());
          newProfile.setPassword(user.getPassword());
          UserDAO.update(newProfile);
          log.info("Successfully update user [" + user.getEmail() + "].");
          user = UserDAO.getUserByEmail(user.getEmail());
          session.setAttribute(Constants.SESSION_KEY_LOGIN_USER, user);
        }
      }
      resp.sendRedirect(Constants.ACCOUNT_PAGE_URL);
    } else {
      resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }
  }
}
